package org.jleaf.springcloud.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yaoshuhong
 * @date 2016-10-09 15:29
 */
@Component
public class MyZuulFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyZuulFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        log.info("访问:{}", request.getRequestURI());

        String uid = request.getParameter("uid");
        if ("a".equals(uid)) {//不放行
            context.setSendZuulResponse(false);
        } else {
            context.addZuulResponseHeader("b", uid);
            context.setSendZuulResponse(true);
        }

        return null;
    }
}
